﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using Csla.Xaml;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaDichVu;
using Accounting.Library.Utility;


namespace csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaHangHoa
{
    public partial class PhanBoChiPhi_ChildWindow: ChildWindow
    {

        public MSP_MuaHangHoa_ViewModel ViewModel;


        public static readonly DependencyProperty TongTienThanhToanProperty =
            DependencyProperty.Register("TongTienThanhToan", typeof(double), typeof(PhanBoChiPhi_ChildWindow), null);

        public double TongTienThanhToan
        {

            get { return (double)GetValue(TongTienThanhToanProperty); }

            set { SetValue(TongTienThanhToanProperty, value); }

        }


        public PhanBoChiPhi_ChildWindow(MSP_MuaHangHoa_ViewModel ViewModel)
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaHangHoa.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            this.ViewModel = ViewModel;
            this.PhanBoChiPhi_GeneralInformation_TongChiPhiMuaHang_TextBox.DataContext = this;
            TongTienThanhToan = 0;
        }
        
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource MSP_MuaDichVu_ListCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaDichVu_ListViewModelSource"];
                MSP_MuaDichVu_ListViewModel msp_MuaDichVu_ListViewModel = new MSP_MuaDichVu_ListViewModel(BusinessConfiguration.Configuration.PhanBoChiPhiMode);
                MSP_MuaDichVu_ListCollectionViewSource.Source = new List<object> { msp_MuaDichVu_ListViewModel };
                this.PhanBoChiPhi_ChiTietGridView.SelectionChanged += new EventHandler<SelectionChangeEventArgs>(OnSelectionChanged);
                
            }
        }

        private void OnSelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            TinhTongTienThanhToan();
        }
       
         private void TinhTongTienThanhToan()
         {
             
             TongTienThanhToan = 0;
             System.Collections.ObjectModel.ObservableCollection<object> list = this.PhanBoChiPhi_ChiTietGridView.SelectedItems ;   
             foreach(object item in list)
             {
                 if( ((MSP_MuaDichVuInfo)item).TongTienHang != null)
                 {
                     TongTienThanhToan += (double)((MSP_MuaDichVuInfo)item).TongTienHang;
                 }
             }
             //this.PhanBoChiPhi_GeneralInformation_TongChiPhiMuaHang_TextBox.Text = this.TongTienThanhToan.ToString();
         }
         private void PhanBoChiPhi_GeneralInformation_PhanBoButon_Click(object sender, RoutedEventArgs e)
         {
             
             if (this.ViewModel != null)
             {
                 if (this.PhanBoChiPhi_GeneralInformation_PhanBoTheoGiaTri_RadioButon.IsChecked == true)
                 {
                     IList<MSP_MuaHangHoa_ChiTietChild> ChiTietList = ViewModel.Model.MSP_MuaHangHoa_ChiTiets;
                     IList<MSP_MuaHangHoa_ChiPhiMuaHangChild> ChiPhiMuaHangList = ViewModel.Model.MSP_MuaHangHoa_ChiPhiMuaHangs; ;
                     int index = 0;
                     foreach (MSP_MuaHangHoa_ChiPhiMuaHangChild item in ChiPhiMuaHangList)
                     {
                         if (ChiTietList[index].ThanhTienQĐ != null && ViewModel.Model.TongTienHang != null && ViewModel.Model.TongTienHang != 0)
                         {
                             double TyLe = (double)(ChiTietList[index].ThanhTienQĐ / ViewModel.Model.TongTienHang);
                             item.SoTienQĐ = TyLe * TongTienThanhToan;
                            
                             if (ViewModel.Model.TyGia != null && ViewModel.Model.TyGia != 0)
                             {
                                 item.SoTien = (double)(item.SoTienQĐ / ViewModel.Model.TyGia);
                             }
                         }
                         index++;

                     }

                 }
                 else if (this.PhanBoChiPhi_GeneralInformation_PhanBoTheoSoLuong_RadioButon.IsChecked == true)
                 {
                     IList<MSP_MuaHangHoa_ChiTietChild> ChiTietList = ViewModel.Model.MSP_MuaHangHoa_ChiTiets;
                     IList<MSP_MuaHangHoa_ChiPhiMuaHangChild> ChiPhiMuaHangList = ViewModel.Model.MSP_MuaHangHoa_ChiPhiMuaHangs; ;
                     int index = 0;
                     double TongSoLuong = 0;
                     foreach (MSP_MuaHangHoa_ChiTietChild item in ChiTietList)
                     {
                         if (item.SoLuong != 0)
                         {
                             TongSoLuong += (double)item.SoLuong;
                         }
                     }
                     foreach (MSP_MuaHangHoa_ChiPhiMuaHangChild item in ChiPhiMuaHangList)
                     {
                         if (ChiTietList[index].SoLuong != null && TongSoLuong != null && TongSoLuong != 0)
                         {
                             double TyLe = (double)(ChiTietList[index].SoLuong / TongSoLuong);
                             item.SoTienQĐ = TyLe * TongTienThanhToan;
                            
                             if (ViewModel.Model.TyGia != null && ViewModel.Model.TyGia != 0)
                             {
                                 item.SoTien = (double)(item.SoTienQĐ / ViewModel.Model.TyGia);
                             }
                         }
                         index++;

                     }

                 }
             }
             this.Close();
         }


    }
}


